import { Component, ElementRef, OnInit, Output, Input, ViewChild, AfterViewInit, EventEmitter } from '@angular/core';
import { HttpModule } from '@angular/http';
import { HttpClientModule, HttpClient, HttpErrorResponse } from '@angular/common/http';

import { RouterModule, Router, ActivatedRoute } from '@angular/router';
import { Service } from '../../server/server';
import { RemoveTableService } from '../../public/serve/remove-table/remove-table.service';
import { Pagination } from '../../public/serve/pcrudInfo';
import { defaultSearchType } from '../../public/serve/pcrudInfo';
import { CudTableService } from '../../public/serve/cud-table/cud-table.service';
import { CentralTableComponent } from '../central/central-table/central-table.component';
declare var $: any;


@Component({
  selector: 'app-central',
  templateUrl: './central.component.html'
})
export class CentralComponent implements OnInit {
  right: any = {
    "system": {},
    "central": {}
  };

  constructor(
    private http: HttpClient, 
    private server: Service, 
    private router: Router, 
    public removeTableService: RemoveTableService, 
    private cudTableService: CudTableService
  ) { 
    this.right = $.extend(this.right, server.getRight());
  }

  public isActive = this.server.isActive;
  //导入table
  @ViewChild("centralTable")centralTable: CentralTableComponent;

  public searchData:centraltype;
  ngOnInit() {
    this.searchData = {
      name: '',
      pageNumber: 1,
      pageSize: 20,
    };
  }

   // 表格
   public totalItems: number;
   ParentList(totalItems: number) {
     this.totalItems = totalItems;
   }

  //新增
  ParentAdd(result_code: string) {
    this.centralTable.getTabList(1, this.pageSize, this.searchData.name);
  } 

  //删除click
  public deletes() {
    $("#delete").modal('show');
  }

  //删除
  ParentDelete(result_code: string) {
    this.centralTable.getTabList(1, this.pageSize, this.searchData.name);
    this.centralTable.cancelSelectAll();
  }

  tableNumber: number = 0;
   // 当前页（默认为第一页）
  private pageNumber: number = 1;
  // 每页显示多少条数据（默认每页显示20条）
  private pageSize: number = 20;
  // id
  private id: string = '0';
  // 分页
  private pagination: Pagination;
  isLoadTableFromPaginationHandler(pagination: Pagination) {
    this.pagination = pagination;

    this.pageNumber = this.pagination[0].pageNumber;
    this.pageSize = this.pagination[0].pageSize;
    this.id = this.pagination[0].id;

    this.tableNumber = (pagination[0].pageNumber - 1) * pagination[0].pageSize;

    this.centralTable.getTabList(this.pageNumber, this.pageSize, this.searchData.name);
  }

  //查询
  search() {
    this.centralTable.getTabList(this.pageNumber, this.pageSize, this.searchData.name);
  }
  //响应搜索
  changesearch() {
    this.search();
  }

  // 删除按钮
  isSelect: boolean = true;
  isSelectFromTableHandler(isSelect: boolean) {
    this.isSelect = isSelect;
  }

}

export class centraltype {
  name: '';
  pageNumber: 1;
  pageSize: 20;
}